# 数据结构

# 1 列表
# This is my shopping list
# shoplist = ['apple', 'mango', 'carrot', 'banana']
# print('I have', len(shoplist), 'items to purchase.')
# print('These items are:', end=' ')
# for item in shoplist:
#     print(item, end=' ')
# print('\nI also have to buy rice.')
# shoplist.append('rice')
# print('My shopping list is now', shoplist)
# print('I will sort my list now')
# shoplist.sort()
# print('Sorted shopping list is', shoplist)
# print('The first item I will buy is', shoplist[0])
# olditem = shoplist[0]
# del shoplist[0]
# print('I bought the', olditem)
# print('My shopping list is now', shoplist)

# myList = ["kobe", "james", 34]
#
# myList.append("12")
# myList.append("zhang")
#
# del myList[1]  # 删除列表中的某一个元素
#
# for it in myList:
#     print(it)

# 2 元组
# 元组（Tuple） 用于将多个对象保存到一起。你可以将它们近似地看作列表，但是元组不能提
# 供列表类能够提供给你的广泛的功能。元组的一大特征类似于字符串，它们是不可变的，也
# 就是说，你不能编辑或更改元组

zoo = ('python', 'elephant', 'penguin')
print('Number of animals in the zoo is', len(zoo))
new_zoo = 'monkey', 'camel', zoo
print('Number of cages in the new zoo is', len(new_zoo))
print('All animals in new zoo are', new_zoo)
print('Animals brought from old zoo are', new_zoo[2])
print('Last animal brought from old zoo is', new_zoo[2][2])
print('Number of animals in the new zoo is', len(new_zoo) - 1 + len(new_zoo[2]))

# 3 字典，类似于 hashmap
# “ab”是地址（Address） 簿（Book） 的缩写
ab = {
    'Swaroop': 'swaroop@swaroopch.com',
    'Larry': 'larry@wall.org',
    'Matsumoto': 'matz@ruby-lang.org',
    'Spammer': 'spammer@hotmail.com'}

print("Swaroop's address is", ab['Swaroop'])

# 删除一对键值—值配对
del ab['Spammer']
print('\nThere are {} contacts in the address-book\n'.format(len(ab)))
for name, address in ab.items():
    print('Contact {} at {}'.format(name, address))
# 添加一对键值—值配对
ab['Guido'] = 'guido@python.org'
if 'Guido' in ab:
    print("\nGuido's address is", ab['Guido'])

# 4 序列 ， 列表，元组，字符串作为某种连续的数据结构，可以应用许多操作符，例如下标，切片(类似于subString)操作符

# shoplist = ['apple', 'mango', 'carrot', 'banana']
# name = 'swaroop'
# # Indexing or 'Subscription' operation #
# # 索引或“下标（Subscription） ”操作符 #
# print('Item 0 is', shoplist[0])
# print('Item 1 is', shoplist[1])
# print('Item 2 is', shoplist[2])
# print('Item 3 is', shoplist[3])
# print("-------------")
# print('Item -1 is', shoplist[-1])  # 逆序下标
# print('Item -2 is', shoplist[-2])
# print('Character 0 is', name[0])
# # Slicing on a list #  列表切片操作符
# print('Item 1 to 3 is', shoplist[1:3])
# print('Item 2 to end is', shoplist[2:])
# print('Item 1 to -1 is', shoplist[1:-1])
# print('Item start to end is', shoplist[:])
# # 从某一字符串中切片 #  字符串切片操作符
# print('characters 1 to 3 is', name[1:3])
# print('characters 2 to end is', name[2:])
# print('characters 1 to -1 is', name[1:-1])
# print('characters start to end is', name[:])


# 5 集合 元素无序，不重复
mySet = set(['brazil', 'russia', 'india'])
print('india' in mySet)

print('usa' in mySet)

newSet = mySet.copy()
newSet.add('china')
newSet.remove('india')
print(newSet.issuperset(mySet))  # 比对连个set之间是否有包含关系
print(mySet & newSet)  # 比对连个set之间拥有的共同元素


# 6 引用
print('Simple Assignment')
shoplist = ['apple', 'mango', 'carrot', 'banana']
# mylist 只是指向同一对象的另一种名称
mylist = shoplist
# 我购买了第一项项目，所以我将其从列表中删除
del shoplist[0]
print('shoplist is', shoplist)
print('mylist is', mylist)
# 注意到 shoplist 和 mylist 二者都
# 打印出了其中都没有 apple 的同样的列表，以此我们确认
# 它们指向的是同一个对象
print('Copy by making a full slice')
# 通过生成一份完整的切片制作一份列表的副本
mylist = shoplist[:]
# 删除第一个项目
del mylist[0]
print('shoplist is', shoplist)
print('mylist is', mylist)
# 注意到现在两份列表已出现不同




